<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <style>
        #app{
            background-color:#ccc;
            padding:10px;
        }
        .child{
            background-color:skyblue;
            padding:10px;
        }
    </style>
</head>
<body>
    <div id="app">
        <p>父组件</p>
        <p>msg:{{msg}}</p>
        <hr>
        <child-one @get-msg="getMsg"></child-one>
        <hr>
        <child-two :msg="msg"></child-two>
    </div>
    <template id="ChildOne">
        <div class="child">
            <p>子组件One</p>
            <p>msg:{{msg}}</p>
            <input type="text" v-model="msg">
            <input type="button" value="传递" @click="sentMsg()">
        </div>
    </template>
    <template id="ChildTwo">
        <div class="child">
            <p>子组件Two</p>
            <p>msg:{{msg}}</p>
        </div>
    </template>
</body>
<script>
    new Vue({
        el:"#app",
        components:{
            ChildOne:{
                template:"#ChildOne",
                data(){
                    return{
                        msg:"组件1的数据"
                    }
                },
                methods:{
                    sentMsg(){
                        this.$emit("get-msg",this.msg);
                    }
                },
                mounted(){
                    this.sentMsg();
                }
            },
            ChildTwo:{
                template:"#ChildTwo",
                props:{
                    msg:String
                }
            }
        },
        data:{
            msg:""
        },
        methods:{
            getMsg(a){
                this.msg=a;
            }
        }
    })
</script>
</html>